# Stage 1: Install dependencies
FROM node:20-slim AS deps
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm npm ci

# Stage 2: Build the application
FROM node:20-slim AS builder
WORKDIR /app

# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Clerk publishable key is read at runtime via bracket notation in the source
# (process.env["NEXT_PUBLIC_CLERK" + "_PUBLISHABLE_KEY"]), which prevents
# Next.js from inlining the value at build time. This enables a single image
# to be promoted across environments without rebuilding.
ENV NEXT_TELEMETRY_DISABLED=1

# Build the Next.js application (standalone output)
RUN npm run build

# Stage 3: Production runtime (standalone ~200MB vs ~1GB with node_modules)
FROM node:20-slim AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV HOSTNAME=0.0.0.0

# Copy standalone output
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

EXPOSE ${PORT:-3000}

CMD ["node", "server.js"]
